// pages/home/home.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		banner: [],
		tarBar: [
			{
				icon: '../../images/icon_a0idh5lo91/sousuo.png',
				text: '搜索'
			},
			{
				icon: '../../images/icon_a0idh5lo91/shipin.png',
				text: '精选视频'
			},
			{
				icon: '../../images/icon_a0idh5lo91/remenhuodong.png',
				text: '热门歌曲'
			},
			{
				icon: '../../images/icon_a0idh5lo91/biaoqiankuozhan_fenlei-202.png',
				text: '分类'
			},
			{
				icon: '../../images/icon_a0idh5lo91/gerenzhongxin.png',
				text: '个人中心'
			},
		],
		//热门歌手
		hotSinger: [],
		//热歌推荐
		hotSongs: [],
		//最新音乐
		newSongs: [],
	},
	//获取首页banner
	getBannerImages() {
		wx.showLoading({
			title: '加载中...',
		})
		wx.cloud.callFunction({
			name: 'getBanner',
		}).then(res2 => {
			wx.hideLoading()
			// console.log(res2);
			this.setData({
				banner: res2.result.data
			})
		}).catch(err => {
			wx.hideLoading()
		})
	},
	//获取热门歌手
	getHotSinger() {
		//alias:不知道是什么
		//fansCount:粉丝数量
		//followed:关注状态
		//id
		//img1v1Url
		//name
		//picUrl
		wx.cloud.callFunction({
			name: 'getHotSinger',
		}).then(res => {
			this.data.hotSinger = res.result.data
			this.setData({
				hotSinger: this.data.hotSinger
			})
		})
	},
	//获取热歌推荐
	getHotSongs() {
		wx.cloud.callFunction({
			name: 'getHotSong'
		}).then(res => {
			this.data.hotSongs = [...res.result.data]
			this.setData({
				hotSongs: this.data.hotSongs
			})
		})
	},
	//获取最新音乐
	getNewSongs() {
		wx.request({
			url: 'http://localhost:3000/personalized/newsong',
			success: res => {
				let result = res.data.result
				// console.log(result);
				result = result.map(item => {
					item = {
						name: item.name,
						id: item.id,
						picUrl: item.picUrl,
						artists: item.song.artists
					}
					return item
				})
				// console.log(result);
				this.setData({
					newSongs: result
				})
			}
		})
	},
	//顶部搜索等路由切换
	changePage(e) {
		let index = e.currentTarget.dataset.index
		if (index == 0) {
			wx.navigateTo({
				url: '../search/search',
			})
		} else if (index == 1) {
			wx.navigateTo({
				url: '../video/video',
			})
		} else if (index == 2) {
			wx.navigateTo({
				url: `../hotMusic/hotMusic?id=${3778678}`,
			})
		} else if (index == 3) {
			wx.navigateTo({
				url: '../category/category',
			})
		} else if (index == 4) {
			wx.navigateTo({
				url: '../my/my',
			})
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getBannerImages()
		this.getHotSinger()
		this.getHotSongs()
		this.getNewSongs()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})